﻿using System;
using Autofac;
using TX.Core;
using TX.Passport.Repositories;

namespace TX.Passport.Persist
{
    public abstract class IocSetting : TX.Passport.IocSetting
    {  
        public new void Register(ContainerBuilder builder, IUnitOfWork unitOfWork)
        {
            RegisterPassportPersistService(builder, unitOfWork);
            RegisterPassportPrincipalResolver(builder, unitOfWork);
            base.Register(builder, unitOfWork);
        }

        /// <summary>
        /// 注册PassportPrincipalResolver
        /// </summary>
        /// <param name="builder"></param>
        /// <param name="unitOfWork"></param>
        public virtual void RegisterPassportPrincipalResolver(ContainerBuilder builder, IUnitOfWork unitOfWork)
        {
            builder.RegisterType<PassportPrincipalResolver>().PropertiesAutowired();
        }

        /// <summary>
        /// 注册PassportPersistService
        /// </summary>
        /// <param name="builder"></param>
        /// <param name="unitOfWork"></param>
        public virtual void RegisterPassportPersistService(ContainerBuilder builder, IUnitOfWork unitOfWork)
        {
            builder.RegisterType<PassportPersistService>().PropertiesAutowired().InstancePerLifetimeScope().OnActivated(t => unitOfWork.AddPart(t.Instance));
        }
        
        #region AdministratorRepository
        /// <summary>
        /// 注册AdministratorRepository
        /// </summary>
        /// <param name="builder"></param>
        public override void RegisterIAdministratorRepository(ContainerBuilder builder)
        {
            builder.RegisterType(GetAdministratorRepository()).As<IAdministratorRepository>().PropertiesAutowired().OnActivated(t => ((AdministratorRepository)t.Instance).PersistService = t.Context.Resolve<PassportPersistService>());
        }
        /// <summary>
        /// AdministratorRepository
        /// </summary>
        /// <returns></returns>
        public override Type GetAdministratorRepository()
        {
            return typeof (AdministratorRepository);
        }
        #endregion
        #region DepartmentRepository
        /// <summary>
        /// 注册DepartmentRepository
        /// </summary>
        /// <param name="builder"></param>
        public override void RegisterIDepartmentRepository(ContainerBuilder builder)
        {
            builder.RegisterType(GetDepartmentRepository()).As<IDepartmentRepository>().PropertiesAutowired().OnActivated(t => ((DepartmentRepository)t.Instance).PersistService = t.Context.Resolve<PassportPersistService>());
        }
        /// <summary>
        /// DepartmentRepository
        /// </summary>
        /// <returns></returns>
        public override Type GetDepartmentRepository()
        {
            return typeof (DepartmentRepository);
        }
        #endregion
        #region PositionRepository
        /// <summary>
        /// 注册PositionRepository
        /// </summary>
        /// <param name="builder"></param>
        public override void RegisterIPositionRepository(ContainerBuilder builder)
        {
            builder.RegisterType(GetPositionRepository()).As<IPositionRepository>().PropertiesAutowired().OnActivated(t => ((PositionRepository)t.Instance).PersistService = t.Context.Resolve<PassportPersistService>());
        }
        /// <summary>
        /// PositionRepository
        /// </summary>
        /// <returns></returns>
        public override Type GetPositionRepository()
        {
            return typeof (PositionRepository);
        }
        #endregion
        #region PrincipalRepository
        /// <summary>
        /// 注册PrincipalRepository
        /// </summary>
        /// <param name="builder"></param>
        public override void RegisterIPrincipalRepository(ContainerBuilder builder)
        {
            builder.RegisterType(GetPrincipalRepository()).As<IPrincipalRepository>().PropertiesAutowired().OnActivated(t => ((PrincipalRepository)t.Instance).PersistService = t.Context.Resolve<PassportPersistService>());
        }
        /// <summary>
        /// PrincipalRepository
        /// </summary>
        /// <returns></returns>
        public override Type GetPrincipalRepository()
        {
            return typeof (PrincipalRepository);
        }
        #endregion
        #region AuthorizationRepository
        /// <summary>
        /// 注册AuthorizationRepository
        /// </summary>
        /// <param name="builder"></param>
        public override void RegisterIAuthorizationRepository(ContainerBuilder builder)
        {
            builder.RegisterType(GetAuthorizationRepository()).As<IAuthorizationRepository>().PropertiesAutowired().OnActivated(t => ((AuthorizationRepository)t.Instance).PersistService = t.Context.Resolve<PassportPersistService>());
        }
        /// <summary>
        /// AuthorizationRepository
        /// </summary>
        /// <returns></returns>
        public override Type GetAuthorizationRepository()
        {
            return typeof (AuthorizationRepository);
        }
        #endregion
        #region OperationRepository
        /// <summary>
        /// 注册OperationRepository
        /// </summary>
        /// <param name="builder"></param>
        public override void RegisterIOperationRepository(ContainerBuilder builder)
        {
            builder.RegisterType(GetOperationRepository()).As<IOperationRepository>().PropertiesAutowired().OnActivated(t => ((OperationRepository)t.Instance).PersistService = t.Context.Resolve<PassportPersistService>());
        }
        /// <summary>
        /// OperationRepository
        /// </summary>
        /// <returns></returns>
        public override Type GetOperationRepository()
        {
            return typeof (OperationRepository);
        }
        #endregion
        #region OperationGroupRepository
        /// <summary>
        /// 注册OperationGroupRepository
        /// </summary>
        /// <param name="builder"></param>
        public override void RegisterIOperationGroupRepository(ContainerBuilder builder)
        {
            builder.RegisterType(GetOperationGroupRepository()).As<IOperationGroupRepository>().PropertiesAutowired().OnActivated(t => ((OperationGroupRepository)t.Instance).PersistService = t.Context.Resolve<PassportPersistService>());
        }
        /// <summary>
        /// OperationGroupRepository
        /// </summary>
        /// <returns></returns>
        public override Type GetOperationGroupRepository()
        {
            return typeof (OperationGroupRepository);
        }
        #endregion
    }
}